/*
 * @(#)BaseExceptionHandler.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.exception;

import java.util.Map;

import com.nhncorp.lucy.common.message.MessageUtil;
import com.nhncorp.usf.core.call.Fault;
import com.nhncorp.usf.core.service.ServiceExecutionContext;
import com.nhncorp.usf.core.util.ErrorUtil;

/**
 * @author Web Platform Development Team
 */
public class BaseExceptionHandler implements ExceptionHandler {

    /**
     * implement ExceptionHandler
     *
     * @param ex      the ex
     * @param context the ServiceExecutionContext
     * @throws Exception the Exception
     */
    public void handle(Exception ex, ServiceExecutionContext context) throws Exception {
    }

    /**
     * implement ExceptionHandler
     *
     * @param fault   the Fault
     * @param context the ServiceExecutionContext
     * @throws Exception the Exception
     */
    public void handle(Fault fault, ServiceExecutionContext context) throws Exception {
        // implement ExceptionHandler
    }

    /**
     * Checks if is user error.
     *
     * @param code the code
     * @return true, if is user error
     */
    public boolean isUserError(String code) {
        UserError[] errors = UserError.values();

        for (UserError error : errors) {
            if (error.equalsCode(code)) {
                return true;
            }
        }

        return false;
    }

    /**
     * Gets the error message.
     *
     * @param ex         the ex
     * @param actionId   the action id
     * @param boId       the bo id
     * @param methodName the method name
     * @param params     the params
     * @return the error message
     */
    protected String getErrorMessage(Exception ex, String actionId, String boId, String methodName,
                                     Map<String, Object> params) {

        return getErrorMessage(ex.getMessage(), getErrorMessage(ex.getMessage()), actionId, boId, methodName, params);
    }

    /**
     * Gets the error message.
     *
     * @param errorCode the error code
     * @return the error message
     */
    protected String getErrorMessage(String errorCode) {
        if (errorCode == null) {
            return "unknown exception";
        }

        String errorMessage = MessageUtil.getMessage(errorCode);
        return errorMessage == null ? "unknown exception" : errorMessage;
    }

    /**
     * Gets the error message.
     *
     * @param fault      the fault
     * @param actionId   the action id
     * @param boId       the bo id
     * @param methodName the method name
     * @param params     the params
     * @return the error message
     */
    protected String getErrorMessage(Fault fault, String actionId, String boId, String methodName,
                                     Map<String, Object> params) {
        String errorCode = fault.getCode();
        String aNew = ErrorUtil.getProperties(fault.getMessage()).get("code");

        if (aNew != null) {
            errorCode = aNew;
        }

        Map<?, ?> detail = (Map<?, ?>)fault.getDetail();
        String message = (String)detail.get("message");

        return getErrorMessage(errorCode + " / " + message, getErrorMessage(errorCode), actionId, boId, methodName,
                               params);
    }

    /**
     * Gets the error message.
     *
     * @param errorCode  the error code
     * @param message    the message
     * @param actionId   the action id
     * @param boId       the bo id
     * @param methodName the method name
     * @param params     the params
     * @return the error message
     */
    protected String getErrorMessage(String errorCode, String message, String actionId, String boId, String methodName,
                                     Map<String, Object> params) {

        StringBuilder sb = new StringBuilder();
        sb.append("\n##############################[USF Error/Exception happens]###########################\n");
        sb.append("[errorCode :").append(errorCode).append(" ]\n");
        sb.append("[errorMessage : ").append(message).append(" ]\n");
        sb.append("[action:").append(actionId).append("]\n");
        sb.append("[businessobject:").append(boId).append("]\n");
        sb.append("[method:").append(methodName).append("]\n");
        sb.append("[parameters :").append(params.toString()).append(" ]\n");
        return sb.toString();
    }
}
